package tt.dz.order.vo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.lang.math.NumberUtils;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.pojo.OrderItem;
import tt.dz.order.util.OrderConstant.CardType;
import tt.dz.order.util.OrderConstant.OrderStatus;
import tt.dz.order.util.OrderConstant.UserType;
import tt.dz.order.util.page.DefaultPaginationBuilder;
import tt.dz.order.util.page.DefaultPaginationDirector;
import tt.dz.order.util.page.Pagination;
import tt.dz.order.util.page.PaginationDirector;

/**
 * 
 * @name 订单查询VO
 * @author Anson
 * @version 1.0
 * @summary
 * @desc
 *
 */
public class OrderQueryVO {

	/**
	 * 订单号:1位用户类型+3位渠道号+14位当前时间+4位随机数
	 */
	private String orderNo;
	/** 订单项id */
	private String itemId;

	/**
	 * 主服务编号,实体服务的父ID
	 */
	private String serviceNo;

	/**
	 * 主服务名称
	 */
	private String serviceName;

	/**
	 * 用户id
	 */
	private String userId;

	/**
	 * 用户名
	 */
	private String userName;

	/**
	 * 卡号
	 */
	private String cardNo;

	/**
	 * 用户类型； 0 个人客户,1 企业客户,2 支持统付的个人客户
	 */
	private String userType;

	/**
	 * 用户类型描述
	 */
	private String userTypeName;

	/**
	 * 卡类型 08 虚拟，06 实体卡 ，07 身份识别卡
	 */
	private String cardType;
	/**
	 * 卡类型描述
	 */
	private String cardTypeName;

	/**
	 * 卡上的余额，如果卡类型是"06 线下卡",需要OMS在第1次调用时传递卡上的余额
	 */
	private String payerBalance;

	/**
	 * 来源系统,用户使用什么app下的单：10001 自主app, 20001 乐视
	 */
	private String appId;

	/**
	 * 订单状态
	 */
	private Integer status;

	/**
	 * 状态秒速描述
	 */
	private String statusName;

	/**
	 * 实际应付的总价
	 */
	private BigDecimal caculatedTotalPrice;

	/**
	 * 支付类型 1 第三方实时支付 2 统付 3 企业支付 4 电子钱包支付 5 代付 6 线下卡
	 */
	private Integer payTypeCode;
	/*-----------Item------*/
	/**
	 * 子服务编号
	 */
	private String serviceSubNo;
	private String stationname;
	private String serviceStationNo;
	private String equipmentno;

	/**
	 * 子服务名称
	 */
	private String serviceSubName;
	/**
	 * 提供服务的运营商编号
	 */
	private String serviceProvider;
	/**
	 * 该运营商提供的服务是否是成本类的服务，0：否 1：是
	 */
	private Integer isCost;
	/**
	 * 计量单位编号
	 */
	private Integer unitNo;
	/**
	 * 原始单价，来源于服务表的unitPrice
	 */
	private BigDecimal unitPrice;
	/**
	 * 实际单价,活动打折后的实际单价
	 */
	private BigDecimal realUnitPrice;
	/**
	 * 购买数量
	 */
	private BigDecimal buyNum;
	/**
	 * 赠送数量
	 */
	private BigDecimal freeNum;
	/**
	 * 原始总价
	 */
	private BigDecimal totalPrice;
	/**
	 * 实际总价
	 */
	private BigDecimal realTotalPrice;
	/**
	 * 订单项描述
	 */
	private String message;
	/**
	 * 是否是orderInfo
	 */
	private boolean orderInfo;
	// 服务开始时间
	private Date startTime;
	// 服务结束时间
	private Date endTime;
	// 创建时间
	private Date createTime;

	public OrderQueryVO() {
		super();
	}

	public String getServiceStationNo() {
		return serviceStationNo;
	}

	public void setServiceStationNo(String serviceStationNo) {
		this.serviceStationNo = serviceStationNo;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public String getServiceNo() {
		return serviceNo;
	}

	public void setServiceNo(String serviceNo) {
		this.serviceNo = serviceNo;
	}

	public String getStationname() {
		return stationname;
	}

	public void setStationname(String stationname) {
		this.stationname = stationname;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getCardNo() {
		return cardNo;
	}

	public String getItemId() {
		return itemId;
	}

	public String getEquipmentno() {
		return equipmentno;
	}

	public void setEquipmentno(String equipmentno) {
		this.equipmentno = equipmentno;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getUserTypeName() {
		return userTypeName;
	}

	public void setUserTypeName(String userTypeName) {
		this.userTypeName = userTypeName;
	}

	public String getCardType() {
		return cardType;
	}

	public void setCardType(String cardType) {
		this.cardType = cardType;
	}

	public String getCardTypeName() {
		return cardTypeName;
	}

	public void setCardTypeName(String cardTypeName) {
		this.cardTypeName = cardTypeName;
	}

	public String getPayerBalance() {
		return payerBalance;
	}

	public void setPayerBalance(String payerBalance) {
		this.payerBalance = payerBalance;
	}

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getStatusName() {
		return statusName;
	}

	public void setStatusName(String statusName) {
		this.statusName = statusName;
	}

	public BigDecimal getCaculatedTotalPrice() {
		return caculatedTotalPrice;
	}

	public void setCaculatedTotalPrice(BigDecimal caculatedTotalPrice) {
		this.caculatedTotalPrice = caculatedTotalPrice;
	}

	public Integer getPayTypeCode() {
		return payTypeCode;
	}

	public void setPayTypeCode(Integer payTypeCode) {
		this.payTypeCode = payTypeCode;
	}

	public String getServiceSubNo() {
		return serviceSubNo;
	}

	public void setServiceSubNo(String serviceSubNo) {
		this.serviceSubNo = serviceSubNo;
	}

	public String getServiceSubName() {
		return serviceSubName;
	}

	public void setServiceSubName(String serviceSubName) {
		this.serviceSubName = serviceSubName;
	}

	public String getServiceProvider() {
		return serviceProvider;
	}

	public void setServiceProvider(String serviceProvider) {
		this.serviceProvider = serviceProvider;
	}

	public Integer getIsCost() {
		return isCost;
	}

	public void setIsCost(Integer isCost) {
		this.isCost = isCost;
	}

	public Integer getUnitNo() {
		return unitNo;
	}

	public void setUnitNo(Integer unitNo) {
		this.unitNo = unitNo;
	}

	public BigDecimal getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(BigDecimal unitPrice) {
		this.unitPrice = unitPrice;
	}

	public BigDecimal getRealUnitPrice() {
		return realUnitPrice;
	}

	public void setRealUnitPrice(BigDecimal realUnitPrice) {
		this.realUnitPrice = realUnitPrice;
	}

	public BigDecimal getBuyNum() {
		return buyNum;
	}

	public void setBuyNum(BigDecimal buyNum) {
		this.buyNum = buyNum;
	}

	public BigDecimal getFreeNum() {
		return freeNum;
	}

	public void setFreeNum(BigDecimal freeNum) {
		this.freeNum = freeNum;
	}

	public BigDecimal getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(BigDecimal totalPrice) {
		this.totalPrice = totalPrice;
	}

	public BigDecimal getRealTotalPrice() {
		return realTotalPrice;
	}

	public void setRealTotalPrice(BigDecimal realTotalPrice) {
		this.realTotalPrice = realTotalPrice;
	}

	public boolean isOrderInfo() {
		return orderInfo;
	}

	public void setOrderInfo(boolean orderInfo) {
		this.orderInfo = orderInfo;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}

	@Override
	public boolean equals(Object obj) {
		return EqualsBuilder.reflectionEquals(this, obj);
	}

	/**
	 * 静态方法:查询当前订单报表相关信息
	 * 
	 * @author Anson
	 * @createDate 2016年4月26日
	 * 
	 * @desc 根据传入进来的消费开始/结束时间,以及当前的服务编号(服务类型),当前卡号,去查询当前关联的订单信息
	 * @param queryOrderResportVO
	 *            服务编号(服务类型)
	 * @return
	 */
	public static List<OrderQueryVO> query(QueryOrderResportVO queryOrderResportVO) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		StringBuilder sbuilder = new StringBuilder();
		sbuilder.append("		SELECT");
		sbuilder.append("		F.orderNo,/*订单号:1位用户类型+3位渠道号+14位当前时间+4位随机数*/");
		sbuilder.append("		F.serviceNo,/* 主服务编号*/");
		sbuilder.append("		F.mainServiceName serviceName,/*主服务名称*/");
		sbuilder.append("		F.userId,/*用户id*/");
		sbuilder.append("		B.name as userName,/*用户名*/");
		sbuilder.append("		F.cardNo,/*卡号*/");
		sbuilder.append("		F.userType,/*用户类型； 0 个人客户,1 企业客户,2 支持统付的个人客户*/");
		sbuilder.append("		F.cardType,/*卡类型 08 虚拟，06 实体卡  ，07  身份识别卡*/");
		sbuilder.append("		F.payerBalance,/*卡上的余额，如果卡类型是\"06 线下卡\",需要OMS在第1次调用时传递卡上的余额*/");
		sbuilder.append("		F.appId,/* 来源系统,用户使用什么app下的单：10001 自主app, 20001 乐视*/");
		sbuilder.append("		F.status,/*订单状态*/");
		sbuilder.append("		F.caculatedTotalPrice,/*实际应付的总价*/");
		sbuilder.append("		F.payTypeCode,/*支付类型 1 第三方实时支付 2 统付 3 企业支付 4 电子钱包支付 5 代付 6 线下卡*/");
		sbuilder.append("		T .serviceNo AS serviceSubNo,/*子服务编号*/");
		sbuilder.append("		I. NAME AS serviceSubName,/*子服务名称*/");
		sbuilder.append("		T .serviceProvider,/*提供服务的运营商编号*/");
		sbuilder.append("		T .isCost,/*该运营商提供的服务是否是成本类的服务，0：否 1：是*/");
		sbuilder.append("		T .unitNo,/*计量单位编号*/");
		sbuilder.append("		T .unitPrice,/*原始单价，来源于服务表的unitPrice*/");
		sbuilder.append("		T .realUnitPrice,/*实际单价,活动打折后的实际单价*/");
		sbuilder.append("		T .buyNum,/*购买数量*/");
		sbuilder.append("		T .freeNum,/*赠送数量*/");
		sbuilder.append("		T .totalPrice,/*原始总价*/");
		sbuilder.append("		T .message,/*订单项描述*/");
		sbuilder.append("		T .realTotalPrice /*实际总价*/");
		sbuilder.append("	FROM");
		sbuilder.append("		order_item T");
		sbuilder.append("	INNER JOIN (");
		sbuilder.append("		SELECT");
		sbuilder.append("			_F.orderNo,");
		sbuilder.append("			_F.serviceNo,");
		sbuilder.append("			_I. NAME AS mainServiceName,");
		sbuilder.append("			_F.userId,");
		sbuilder.append("			_F.cardNo,");
		sbuilder.append("			_F.userType,");
		sbuilder.append("			_F.cardType,");
		sbuilder.append("			_F.payerBalance,");
		sbuilder.append("			_F.appId,");
		sbuilder.append("			_F.status,");
		sbuilder.append("			_F.caculatedTotalPrice,");
		sbuilder.append("			_F.payTypeCode");
		sbuilder.append("		FROM");
		sbuilder.append("			order_info _F");
		sbuilder.append("		LEFT JOIN order_service_info _I ON _I.serviceno = _F.serviceNo");
		sbuilder.append("		WHERE");
		sbuilder.append("			1 = 1");
		if (queryOrderResportVO != null) {
			if (!StringUtils.isEmpty(queryOrderResportVO.getServiceNo())) {
				sbuilder.append("		AND _F.serviceNo =:serviceNo");
				paramMap.put("serviceNo", queryOrderResportVO.getServiceNo());
			}
			if (!StringUtils.isEmpty(queryOrderResportVO.getCardNo())) {
				sbuilder.append("		AND _F.cardno =:cardNo");
				paramMap.put("cardNo", queryOrderResportVO.getCardNo());
			}
			if (queryOrderResportVO.getStartDate() != null) {
				sbuilder.append("		AND _F.createTime >=:startDate");
				paramMap.put("startDate", queryOrderResportVO.getStartDate());
			}
			if (queryOrderResportVO.getEndDate() != null) {
				sbuilder.append("		AND _F.createTime <=:endDate");
				paramMap.put("endDate", queryOrderResportVO.getEndDate());
			}
		}
		sbuilder.append("	) F ON F.orderno = T .orderno");
		sbuilder.append("	LEFT JOIN order_service_info I ON I.serviceno = T .serviceNo ");
		sbuilder.append(" LEFT JOIN ms_user_base B on B.user_id = T.userid ");
		List<OrderQueryVO> list = BaseDAOUtil.queryForList(sbuilder.toString(), paramMap, OrderQueryVO.class);
		// 转换其中的基础代码转换为名称
		if (!CollectionUtils.isEmpty(list)) {
			for (OrderQueryVO orderQueryVO : list) {
				if (!StringUtils.isEmpty(orderQueryVO.getUserType())
						&& NumberUtils.isDigits(orderQueryVO.getUserType())) {
					orderQueryVO
							.setUserTypeName(UserType.indexOf(NumberUtils.toInt(orderQueryVO.getUserType())).desc());
				}
				if (!StringUtils.isEmpty(orderQueryVO.getCardType())) {
					orderQueryVO.setCardTypeName(CardType.indexOf(orderQueryVO.getCardType()).getDesc());
				}
				if (orderQueryVO.getStatus() != null) {
					orderQueryVO.setStatusName(OrderStatus.getDesc(orderQueryVO.getStatus()));
				}
			}
		}

		return list;
	}

	public static Pagination<OrderQueryVO> queryPagition(QueryOrderResportVO queryOrderResportVO, int curPage) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		StringBuilder sbuilder = new StringBuilder();
		sbuilder.append("		SELECT");
		sbuilder.append("		F.orderNo,/*订单号:1位用户类型+3位渠道号+14位当前时间+4位随机数*/");
		sbuilder.append("		F.serviceNo,/* 主服务编号*/");
		sbuilder.append("		F.mainServiceName serviceName,/*主服务名称*/");
		sbuilder.append("		F.userId,/*用户id*/");
		sbuilder.append("		B.name as userName,/*用户名*/");
		sbuilder.append("		F.cardNo,/*卡号*/");
		sbuilder.append("		F.userType,/*用户类型； 0 个人客户,1 企业客户,2 支持统付的个人客户*/");
		sbuilder.append("		F.cardType,/*卡类型 08 虚拟，06 实体卡  ，07  身份识别卡*/");
		sbuilder.append("		F.payerBalance,/*卡上的余额，如果卡类型是\"06 线下卡\",需要OMS在第1次调用时传递卡上的余额*/");
		sbuilder.append("		F.appId,/* 来源系统,用户使用什么app下的单：10001 自主app, 20001 乐视*/");
		sbuilder.append("		F.status,/*订单状态*/");
		sbuilder.append("		F.caculatedTotalPrice,/*实际应付的总价*/");
		sbuilder.append("		F.payTypeCode,/*支付类型 1 第三方实时支付 2 统付 3 企业支付 4 电子钱包支付 5 代付 6 线下卡*/");
		sbuilder.append("		T .serviceNo AS serviceSubNo,/*子服务编号*/");
		sbuilder.append("		I. NAME AS serviceSubName,/*子服务名称*/");
		sbuilder.append("		T .serviceProvider,/*提供服务的运营商编号*/");
		sbuilder.append("		T .isCost,/*该运营商提供的服务是否是成本类的服务，0：否 1：是*/");
		sbuilder.append("		T .unitNo,/*计量单位编号*/");
		sbuilder.append("		T .unitPrice,/*原始单价，来源于服务表的unitPrice*/");
		sbuilder.append("		T .realUnitPrice,/*实际单价,活动打折后的实际单价*/");
		sbuilder.append("		T .buyNum,/*购买数量*/");
		sbuilder.append("		T .freeNum,/*赠送数量*/");
		sbuilder.append("		T .totalPrice,/*原始总价*/");
		sbuilder.append("		T .message,/*订单项描述*/");
		sbuilder.append("		T .realTotalPrice /*实际总价*/");
		sbuilder.append("	FROM");
		sbuilder.append("		order_item T");
		sbuilder.append("	INNER JOIN (");
		sbuilder.append("		SELECT");
		sbuilder.append("			_F.orderNo,");
		sbuilder.append("			_F.serviceNo,");
		sbuilder.append("			_I. NAME AS mainServiceName,");
		sbuilder.append("			_F.userId,");
		sbuilder.append("			_F.cardNo,");
		sbuilder.append("			_F.userType,");
		sbuilder.append("			_F.cardType,");
		sbuilder.append("			_F.payerBalance,");
		sbuilder.append("			_F.appId,");
		sbuilder.append("			_F.status,");
		sbuilder.append("			_F.caculatedTotalPrice,");
		sbuilder.append("			_F.payTypeCode");
		sbuilder.append("		FROM");
		sbuilder.append("			order_info _F");
		sbuilder.append("		LEFT JOIN order_service_info _I ON _I.serviceno = _F.serviceNo");
		sbuilder.append("		WHERE");
		sbuilder.append("			1 = 1");
		if (queryOrderResportVO != null) {
			if (!StringUtils.isEmpty(queryOrderResportVO.getServiceNo())) {
				sbuilder.append("		AND _F.serviceNo =:serviceNo");
				paramMap.put("serviceNo", queryOrderResportVO.getServiceNo());
			}
			if (!StringUtils.isEmpty(queryOrderResportVO.getCardNo())) {
				sbuilder.append("		AND _F.cardno =:cardNo");
				paramMap.put("cardNo", queryOrderResportVO.getCardNo());
			}
			if (queryOrderResportVO.getStartDate() != null) {
				sbuilder.append("		AND _F.createTime >=:startDate");
				paramMap.put("startDate", queryOrderResportVO.getStartDate());
			}
			if (queryOrderResportVO.getEndDate() != null) {
				sbuilder.append("		AND _F.createTime <=:endDate");
				paramMap.put("endDate", queryOrderResportVO.getEndDate());
			}
		}
		sbuilder.append("	) F ON F.orderno = T .orderno");
		sbuilder.append("	LEFT JOIN order_service_info I ON I.serviceno = T .serviceNo ");
		sbuilder.append(" LEFT JOIN ms_user_base B on B.user_id = T.userid ");
		List<OrderQueryVO> list = BaseDAOUtil.queryForList(sbuilder.toString(), paramMap, OrderQueryVO.class);
		// 转换其中的基础代码转换为名称
		if (!CollectionUtils.isEmpty(list)) {
			for (OrderQueryVO orderQueryVO : list) {
				if (!StringUtils.isEmpty(orderQueryVO.getUserType())
						&& NumberUtils.isDigits(orderQueryVO.getUserType())) {
					orderQueryVO
							.setUserTypeName(UserType.indexOf(NumberUtils.toInt(orderQueryVO.getUserType())).desc());
				}
				if (!StringUtils.isEmpty(orderQueryVO.getCardType())) {
					orderQueryVO.setCardTypeName(CardType.indexOf(orderQueryVO.getCardType()).getDesc());
				}
				if (orderQueryVO.getStatus() != null) {
					orderQueryVO.setStatusName(OrderStatus.getDesc(orderQueryVO.getStatus()));
				}
			}
		}

		Pagination<OrderQueryVO> model = new Pagination<OrderQueryVO>();
		return model;
	}

	/**
	 * 订单查询(不管是否发送台账信息)
	 * 
	 * @param queryOrderResportVO
	 * @param curPageNo
	 * @param prePageNum
	 * @return
	 */
	public static Pagination<OrderQueryVO> queryPagition(QueryOrderResportVO queryOrderResportVO, int curPageNo,
			int prePageNum) {
		return queryPagition(queryOrderResportVO, curPageNo, prePageNum, true);
	}

	/**
	 * 订单查询
	 * 
	 * @param queryOrderResportVO
	 * @param curPageNo
	 * @param prePageNum
	 * @param waSentAccounting
	 *            false 时只查询未发送台账的订单
	 * @return
	 */
	public static Pagination<OrderQueryVO> queryPagition(QueryOrderResportVO queryOrderResportVO, int curPageNo,
			int prePageNum, boolean waSentAccounting) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		StringBuilder controlBuilder = new StringBuilder();
//		if(queryOrderResportVO.getMerchantCode()==null||"".equals(queryOrderResportVO.getMerchantCode())){
//			controlBuilder.append(
//					" FROM    order_info F LEFT JOIN order_service_info _I ON _I.serviceno = F.serviceno   LEFT JOIN ms_user_base B on B.user_id = F.userid   WHERE    1 = 1 ");
//		}else{
			controlBuilder.append(
					" FROM    order_info F INNER join ( SELECT  serviceprovider, orderno from order_item  where 1=1  ");
//			private String cardTypeNo;
//			private String serviceStationNo;
//			private String equipmentNo;

		if (!StringUtils.isEmpty(queryOrderResportVO.getServiceStationNo())) {
			controlBuilder.append("		AND serviceStationNo =:serviceStationNo");
			paramMap.put("serviceStationNo", queryOrderResportVO.getServiceStationNo());
		}
		if (!StringUtils.isEmpty(queryOrderResportVO.getEquipmentNo())) {
			controlBuilder.append("		AND  equipmentNo =:equipmentNo");
			paramMap.put("equipmentNo", queryOrderResportVO.getEquipmentNo());
		}
		if (!StringUtils.isEmpty(queryOrderResportVO.getMerchantCode())) {
			controlBuilder.append("		AND  serviceprovider =:merchantCode");
			paramMap.put("merchantCode", queryOrderResportVO.getMerchantCode());
		}
			controlBuilder.append(" GROUP BY serviceprovider,orderno ) cod on F.orderno=cod.orderno LEFT JOIN order_service_info _I ON _I.serviceno = F.serviceno   LEFT JOIN ms_user_base B on B.user_id = F.userid   WHERE    1 = 1");
//		}

		if (queryOrderResportVO != null) {

			if (!StringUtils.isEmpty(queryOrderResportVO.getServiceNo())) {
				controlBuilder.append("		AND F.serviceno =:serviceNo");
				paramMap.put("serviceNo", queryOrderResportVO.getServiceNo());
			}
			if (!StringUtils.isEmpty(queryOrderResportVO.getCardTypeNo())) {
				controlBuilder.append("		AND F.cardType =:cardType");
				paramMap.put("cardType", queryOrderResportVO.getCardTypeNo());
			}

			if (!StringUtils.isEmpty(queryOrderResportVO.getCardNo())) {
				controlBuilder.append("		AND F.cardno =:cardNo");
				paramMap.put("cardNo", queryOrderResportVO.getCardNo());
			}
			if (queryOrderResportVO.getStartDate() != null) {
				controlBuilder.append("		AND F.createtime >=:startDate");
				paramMap.put("startDate", queryOrderResportVO.getStartDate());
			}
			if (queryOrderResportVO.getEndDate() != null) {
				controlBuilder.append("		AND F.createtime <=:endDate");
				paramMap.put("endDate", queryOrderResportVO.getEndDate());
			}
			if (!StringUtils.isEmpty(queryOrderResportVO.getOrderNo())) {
				controlBuilder.append("		AND F.orderno =:orderno");
				paramMap.put("orderno", queryOrderResportVO.getOrderNo());
			}
			if (null != queryOrderResportVO.getStatus()) {
				controlBuilder.append("		AND F.status =:status");
				paramMap.put("status", queryOrderResportVO.getStatus());
			}
		}
		if (!waSentAccounting) {
			controlBuilder.append(
					"		AND  0=(SELECT count(1) FROM Order_Accounting_Status accounting where accounting.orderno =:orderno)");
		}
		String sql = "SELECT    F.orderno,    F.serviceNo,    _I. NAME AS serviceName,    B.mobile_no as userName,    F.cardNo,    F.userType,    F.cardType,    F.payerBalance,    F.appId,    F.status,    F.caculatedTotalPrice,    F.payTypeCode ,F.createtime"
				+ controlBuilder.toString();
		sql+=" order by F.createtime desc"; 
		String countSql = " select count( F.orderno) " + controlBuilder.toString();
		PaginationDirector director = new DefaultPaginationDirector(new DefaultPaginationBuilder(), countSql, sql,
				paramMap, curPageNo, prePageNum);
		Pagination<OrderQueryVO> pagination = director.construct(OrderQueryVO.class);
		List<OrderQueryVO> list = pagination.getList();
		if (!CollectionUtils.isEmpty(list)) {
			for (OrderQueryVO orderQueryVO : list) {
				if (!StringUtils.isEmpty(orderQueryVO.getUserType())
						&& NumberUtils.isDigits(orderQueryVO.getUserType())) {
					orderQueryVO
							.setUserTypeName(UserType.indexOf(NumberUtils.toInt(orderQueryVO.getUserType())).desc());
				}
				if (!StringUtils.isEmpty(orderQueryVO.getCardType())) {
					orderQueryVO.setCardTypeName(CardType.indexOf(orderQueryVO.getCardType()).getDesc());
				}
				if (orderQueryVO.getStatus() != null) {
					orderQueryVO.setStatusName(OrderStatus.getDesc(orderQueryVO.getStatus()));
				}
			}
			queryOrderItembyOrderQueryVOList(pagination);
		}
		return pagination;
	}
	/**
	 * 导出
	 *
	 * @param queryOrderResportVO
	 *            false 时只查询未发送台账的订单
	 * @return
	 */
	public static List<ExportOrderItem> export(QueryOrderResportVO queryOrderResportVO) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		StringBuilder controlBuilder = new StringBuilder();
		controlBuilder.append(
				" SELECT F.cardno,F.cardtype, item.orderno,item.stationname,item.servicestationno, item.equipmentno ,_I.name,item.buynum, item.totalprice,item.starttime,item.endtime FROM order_item item INNER JOIN order_info F ON F.orderno = item.orderno LEFT JOIN order_service_info _I ON _I.serviceno = item.serviceno where 1=1 ") ;
		if (queryOrderResportVO != null) {
			if (!StringUtils.isEmpty(queryOrderResportVO.getServiceStationNo())) {
				controlBuilder.append("		AND serviceStationNo =:serviceStationNo");
				paramMap.put("serviceStationNo", queryOrderResportVO.getServiceStationNo());
			}
			if (!StringUtils.isEmpty(queryOrderResportVO.getEquipmentNo())) {
				controlBuilder.append("		AND  equipmentNo =:equipmentNo");
				paramMap.put("equipmentNo", queryOrderResportVO.getEquipmentNo());
			}
			if (!StringUtils.isEmpty(queryOrderResportVO.getServiceNo())) {
				controlBuilder.append("		AND F.serviceno =:serviceNo");
				paramMap.put("serviceNo", queryOrderResportVO.getServiceNo());
			}
			if (!StringUtils.isEmpty(queryOrderResportVO.getCardTypeNo())) {
				controlBuilder.append("		AND F.cardType =:cardType");
				paramMap.put("cardType", queryOrderResportVO.getCardTypeNo());
			}
			if (!StringUtils.isEmpty(queryOrderResportVO.getMerchantCode())) {
				paramMap.put("merchantCode", queryOrderResportVO.getMerchantCode());
			}
			if (!StringUtils.isEmpty(queryOrderResportVO.getCardNo())) {
				controlBuilder.append("		AND F.cardno =:cardNo");
				paramMap.put("cardNo", queryOrderResportVO.getCardNo());
			}
			if (queryOrderResportVO.getStartDate() != null) {
				controlBuilder.append("		AND F.createtime >=:startDate");
				paramMap.put("startDate", queryOrderResportVO.getStartDate());
			}
			if (queryOrderResportVO.getEndDate() != null) {
				controlBuilder.append("		AND F.createtime <=:endDate");
				paramMap.put("endDate", queryOrderResportVO.getEndDate());
			}
			if (!StringUtils.isEmpty(queryOrderResportVO.getOrderNo())) {
				controlBuilder.append("		AND F.orderno =:orderno");
				paramMap.put("orderno", queryOrderResportVO.getOrderNo());
			}
			if (null != queryOrderResportVO.getStatus()) {
				controlBuilder.append("		AND F.status =:status");
				paramMap.put("status", queryOrderResportVO.getStatus());
			}
		}
		return BaseDAOUtil.queryForList(controlBuilder.toString(), paramMap, ExportOrderItem.class);
	}
	/**
	 * 根据订单列表,查询对应订单项信息
	 * @param pagination
	 */
	public static void queryOrderItembyOrderQueryVOList(Pagination<OrderQueryVO> pagination) {
		List<OrderQueryVO> list = pagination.getList();
		if (!CollectionUtils.isEmpty(list)) {
			StringBuilder builder = new StringBuilder(
					"SELECT X.itemId, X.startTime, X.endTime, X.createTime,X.stationname,X.serviceStationNo,X.equipmentno, X.orderNo, T.Name as serviceSubName, T.serviceNo as serviceSubNo,X.serviceProvider,X.buynum,X.unitPrice,X.totalprice,X.realtotalprice,X.message FROM order_item X LEFT JOIN order_service_info T ON X.serviceno = T.serviceno WHERE orderno IN ( ");
			for (OrderQueryVO orderQueryVO : list) {
				orderQueryVO.setOrderInfo(true);
				builder.append("'" + orderQueryVO.getOrderNo() + "',");
			}
			builder.deleteCharAt(builder.length() - 1);
			builder.append(")");
			List<OrderQueryVO> OrderQueryVOlist = BaseDAOUtil.queryForList(builder.toString(), null,
					OrderQueryVO.class);
			// list.addAll(OrderQueryVOlist);
			pagination.setOrderItemlist(OrderQueryVOlist);
		}
	}
	/**
	 * 根据订单项id查询对应订单项信息
	 * @param orderItem
	 * @return
	 */
	public static OrderQueryVO queryOrderItembyOrderItemId(OrderItem orderItem) {
		StringBuilder builder = new StringBuilder(
				"SELECT X.itemId, X.startTime, X.endTime, X.createTime, X.orderNo, T.Name as serviceSubName, T.serviceNo as serviceSubNo,X.serviceProvider,X.buynum,X.unitPrice,X.totalprice,X.realtotalprice,X.message FROM order_item X LEFT JOIN order_service_info T ON X.serviceno = T.serviceno WHERE X.itemId=:itemId ");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("itemId", orderItem.getItemId());
		OrderQueryVO orderQueryVO = BaseDAOUtil.queryForObject(builder.toString(), params, OrderQueryVO.class);
		// list.addAll(OrderQueryVOlist);
		return orderQueryVO;

	}
}